%% Training and validation sets
x = Xtrainset';%[0 0 1 1;0 1 0 1
t = Ytrainset';%[-1 1 1 -1];
x = x(:,1:100);
t = t(1:100);
x2 = Xvalidationset';%[0 0 1 1;0 1 0 1
t2 = Yvalidationset';%[-1 1 1 -1];
Nf = size(x,1);
Ntrain = size(x,2);
Nval = size(x2,2);

%% Initialize MLP
h1 = 4;
W1 = normrnd(0, 0.1, 2*h1, Nf);
b1 = normrnd(0, 0.1, 2*h1, 1);
W2 = normrnd(0, 0.1, 1, h1);
b2 = normrnd(0, 0.1);
deltaW1 = rand(2*h1, Nf)*0.001;
deltab1 = rand(2*h1, 1)*0.001;
deltaW2 = rand(1, h1)*0.001;
deltab2 = rand*0.001;

%% Data to test
x_ = x;
t_ = t;

[out gradW1 gradb1 gradW2 gradb2] = MLP(x_, t_, W1, b1, W2, b2);
E1 = logErr(out, t_);

deltaE = [gradW1(:);gradb1(:);gradW2(:);gradb2(:)]' * [deltaW1(:);deltab1(:);deltaW2(:);deltab2(:)];

[out gradW1 gradb1 gradW2 gradb2] = MLP(x_, t_, W1+deltaW1, b1+deltab1, W2+deltaW2, b2+deltab2);
deltaE2 = logErr(out, t_) - E1;

fprintf('%f :: %f\n', deltaE, deltaE2);

